<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next-haha.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next-haha.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next-haha.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.0/css/all.min.css" integrity="sha256-VHqXKFhhMxcpubYf9xiWdCiojEbY9NexQ4jh8AxbvcM=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"ming.theyan.gs","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.25.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"codeblock":{"theme":{"light":"default","dark":"stackoverflow-dark"},"prism":{"light":"prism","dark":"prism-dark"},"copy_button":{"enable":false,"style":null},"fold":{"enable":false,"height":500},"language":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false,"trigger":"auto"}}</script><script src="/js/config.js" defer></script>

    <meta name="description" content="发现问题最近在“不务正业”，应老板需求，给我极的极 go 写一个插件，方便公司员工方便地登录 VPN。关于这个插件的事情另外单说，我这里主要讲下开发、调试这个插件期间发现的另外一个问题。 我在 OpenVPN 服务器上编写认证鉴权程序的时候，需要频繁查看 OpenVPN 的日志，我的 OpenVPN 日志文件是用参数 log 而非 log-append 指定的。">
<meta property="og:type" content="article">
<meta property="og:title" content="从一次用cat空文件到log文件来清log方法的失败说开去">
<meta property="og:url" content="https://ming.theyan.gs/2016/11/%E4%BB%8E%E4%B8%80%E6%AC%A1%E7%94%A8cat%E7%A9%BA%E6%96%87%E4%BB%B6%E5%88%B0log%E6%96%87%E4%BB%B6%E6%9D%A5%E6%B8%85log%E6%96%B9%E6%B3%95%E7%9A%84%E5%A4%B1%E8%B4%A5%E8%AF%B4%E5%BC%80%E5%8E%BB/index.html">
<meta property="og:site_name" content="运维烂笔头">
<meta property="og:description" content="发现问题最近在“不务正业”，应老板需求，给我极的极 go 写一个插件，方便公司员工方便地登录 VPN。关于这个插件的事情另外单说，我这里主要讲下开发、调试这个插件期间发现的另外一个问题。 我在 OpenVPN 服务器上编写认证鉴权程序的时候，需要频繁查看 OpenVPN 的日志，我的 OpenVPN 日志文件是用参数 log 而非 log-append 指定的。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2016-11-19T09:18:52.000Z">
<meta property="article:modified_time" content="2019-04-04T23:47:07.020Z">
<meta property="article:author" content="老杨">
<meta property="article:tag" content="CentOS">
<meta property="article:tag" content="Linux">
<meta property="article:tag" content="OpenVPN">
<meta property="article:tag" content="log">
<meta property="article:tag" content="log-append">
<meta property="article:tag" content="O_APPEND">
<meta property="article:tag" content="O_WRONLY">
<meta property="article:tag" content="O_LARGEFILE">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://ming.theyan.gs/2016/11/%E4%BB%8E%E4%B8%80%E6%AC%A1%E7%94%A8cat%E7%A9%BA%E6%96%87%E4%BB%B6%E5%88%B0log%E6%96%87%E4%BB%B6%E6%9D%A5%E6%B8%85log%E6%96%B9%E6%B3%95%E7%9A%84%E5%A4%B1%E8%B4%A5%E8%AF%B4%E5%BC%80%E5%8E%BB/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://ming.theyan.gs/2016/11/%E4%BB%8E%E4%B8%80%E6%AC%A1%E7%94%A8cat%E7%A9%BA%E6%96%87%E4%BB%B6%E5%88%B0log%E6%96%87%E4%BB%B6%E6%9D%A5%E6%B8%85log%E6%96%B9%E6%B3%95%E7%9A%84%E5%A4%B1%E8%B4%A5%E8%AF%B4%E5%BC%80%E5%8E%BB/index.html","path":"2016/11/从一次用cat空文件到log文件来清log方法的失败说开去/index.html","title":"从一次用cat空文件到log文件来清log方法的失败说开去"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>从一次用cat空文件到log文件来清log方法的失败说开去 | 运维烂笔头</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-106574959-2"></script>
  <script class="next-config" data-name="google_analytics" type="application/json">{"tracking_id":"UA-106574959-2","only_pageview":false,"measure_protocol_api_secret":null}</script>
  <script src="/js/third-party/analytics/google-analytics.js" defer></script>

  <script src="/js/third-party/analytics/baidu-analytics.js" defer></script>
  <script async src="https://hm.baidu.com/hm.js?fdef9ded31bdb8b2dab08eddebdd5fed"></script>







  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous" defer></script>
<script src="/js/utils.js" defer></script><script src="/js/motion.js" defer></script><script src="/js/sidebar.js" defer></script><script src="/js/next-boot.js" defer></script><script src="/js/bookmark.js" defer></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.5.0/search.js" integrity="sha256-xFC6PJ82SL9b3WkGjFavNiA9gm5z6UBxWPiu4CYjptg=" crossorigin="anonymous" defer></script>
<script src="/js/third-party/search/local-search.js" defer></script>







  




<!-- google adsense -->
<script data-ad-client="ca-pub-1045025618858716" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="运维烂笔头" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">运维烂笔头</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">一个 SA 老兵的工作日志</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-projects"><a href="/projects" rel="section"><i class="fa fa-code fa-fw"></i>projects</a></li><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8F%91%E7%8E%B0%E9%97%AE%E9%A2%98"><span class="nav-number">1.</span> <span class="nav-text">发现问题</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%88%86%E6%9E%90%E5%8E%9F%E5%9B%A0"><span class="nav-number">2.</span> <span class="nav-text">分析原因</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%BB%B6%E5%B1%95%E8%AF%9D%E9%A2%98"><span class="nav-number">3.</span> <span class="nav-text">延展话题</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-number">4.</span> <span class="nav-text">参考</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">老杨</p>
  <div class="site-description" itemprop="description">好记性比不过烂笔头</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">114</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">509</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/haw-haw" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;haw-haw" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:blog@theyan.gs" title="E-Mail → mailto:blog@theyan.gs" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/1494877243" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;1494877243" rel="noopener me" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://twitter.com/6fool" title="Twitter → https:&#x2F;&#x2F;twitter.com&#x2F;6fool" rel="noopener me" target="_blank"><i class="fab fa-twitter fa-fw"></i>Twitter</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          链接
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <a href="https://bad-pencil.github.io/" title="https:&#x2F;&#x2F;bad-pencil.github.io" rel="noopener" target="_blank">github 镜像站</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://hawhaw.gitee.io/" title="https:&#x2F;&#x2F;hawhaw.gitee.io" rel="noopener" target="_blank">gitee 镜像站</a>
            </li>
        </ul>
      </div>
    </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ming.theyan.gs/2016/11/%E4%BB%8E%E4%B8%80%E6%AC%A1%E7%94%A8cat%E7%A9%BA%E6%96%87%E4%BB%B6%E5%88%B0log%E6%96%87%E4%BB%B6%E6%9D%A5%E6%B8%85log%E6%96%B9%E6%B3%95%E7%9A%84%E5%A4%B1%E8%B4%A5%E8%AF%B4%E5%BC%80%E5%8E%BB/index.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="老杨">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="运维烂笔头">
      <meta itemprop="description" content="好记性比不过烂笔头">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="从一次用cat空文件到log文件来清log方法的失败说开去 | 运维烂笔头">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          从一次用cat空文件到log文件来清log方法的失败说开去
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2016-11-19 17:18:52" itemprop="dateCreated datePublished" datetime="2016-11-19T17:18:52+08:00">2016-11-19</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2019-04-05 07:47:07" itemprop="dateModified" datetime="2019-04-05T07:47:07+08:00">2019-04-05</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h1 id="发现问题"><a href="#发现问题" class="headerlink" title="发现问题"></a>发现问题</h1><p>最近在“不务正业”，应老板需求，给我极的极 go 写一个插件，方便公司员工方便地登录 VPN。关于这个插件的事情另外单说，我这里主要讲下开发、调试这个插件期间发现的另外一个问题。</p>
<p>我在 OpenVPN 服务器上编写认证鉴权程序的时候，需要频繁查看 OpenVPN 的日志，我的 OpenVPN 日志文件是用参数 log 而非 log-append 指定的。</p>
<a id="more"></a>
<p>某日，我基于以下几个原因想把日志文件不停服务清一下：</p>
<ul>
<li>文件很大，不方便打开来看</li>
<li>日志打的很细，所以增长很快</li>
<li>自己要的日志特征也不明显，不方便用 grep 来过滤</li>
</ul>
<p>所以，基于 Linux 系统管理员的基本常识，直接</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">: &gt; <span class="built_in">log</span>;</span><br></pre></td></tr></table></figure>
<p>但是当我 1 秒钟后（几乎也就是马上）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ls -l <span class="built_in">log</span>;</span><br></pre></td></tr></table></figure>
<p>发现这个文件的大小比清之前又大了一些！然后又做过几次测试，具体就不赘述了，反正现象就是：当清 log 的当时，文件大小是变为 0 了的，但是几乎是马上，文件立马又恢复到清之前的大小还大一点，仿佛就是跟没有执行清除 log 文件操作一样！</p>
<h1 id="分析原因"><a href="#分析原因" class="headerlink" title="分析原因"></a>分析原因</h1><p>最早我以为是内容没有及时刷到硬盘上的原因，于是在清日志操作前后加上了</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sync;</span><br></pre></td></tr></table></figure>
<p>结果发现还是一样的。于是就大概网上扫了下，发现是日志文件 log 的打开方式所致。具体情况我们通过一点实验来演示下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">exec</span> 3&gt;<span class="built_in">log</span>;</span><br><span class="line"><span class="comment"># 用文件描述符33来打开文件 log</span></span><br><span class="line"><span class="built_in">printf</span> zzz &gt;&amp;3;</span><br><span class="line"><span class="comment"># 把字符串 "zzz" 写入文件描述符 33 打开的文件（也就是文件 log）</span></span><br><span class="line"><span class="comment"># 注意：此时的光标在文件的第四个位置（最右边那个字母 "z" 的右边）</span></span><br><span class="line">ls -l <span class="built_in">log</span>;od -c <span class="built_in">log</span>;</span><br><span class="line"><span class="comment"># 查看文件大小及其内容</span></span><br></pre></td></tr></table></figure>
<p>系统输出：</p>
<blockquote>
<p>-rw-r–r– 1 root root 3 Nov 23 16:00 log<br>0000000   z   z   z<br>0000003</p>
</blockquote>
<p>可以看到，文件 log 的大小是 3 字节、内容为 “zzz”，接着做：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span> aaaa &gt;&gt; <span class="built_in">log</span>;</span><br><span class="line"><span class="comment"># 通过另外一个文件描述符打开文件 log，</span></span><br><span class="line"><span class="comment"># 并在其后追加了字符串 "aaaa"</span></span><br><span class="line"><span class="comment"># 注意：这里并没有修改文件描述符 3 光标的位置！</span></span><br><span class="line"><span class="comment"># 文件描述符 3 的光标还在第四个字符的位置</span></span><br><span class="line"><span class="comment"># 也就是最左边那个字符 "a" 的位置</span></span><br><span class="line">ls -l <span class="built_in">log</span>;od -c <span class="built_in">log</span>;</span><br><span class="line"><span class="comment"># 再次查看文件 log 的大小和内容</span></span><br></pre></td></tr></table></figure>
<p>系统输出：</p>
<blockquote>
<p>-rw-r–r– 1 root root 7 Nov 23 16:11 log<br>0000000   z   z   z   a   a   a   a<br>0000007</p>
</blockquote>
<p>由此可知现在文件 log 的大小为 7 字节，内容是 “zzzaaaa”，再接着做实验：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span> bb &gt;&amp;3;</span><br><span class="line"><span class="comment"># 在文件描述符 3 的光标位置开始写入字符串 "bb"</span></span><br><span class="line"><span class="comment"># 注意：写完后光标到了第 6 个字符的位置</span></span><br><span class="line"><span class="comment"># 也就是左边那个字符 "a" 的位置</span></span><br><span class="line">ls -l <span class="built_in">log</span>;od -c <span class="built_in">log</span>;</span><br><span class="line"><span class="comment"># 再次查看文件 log 的大小和内容</span></span><br></pre></td></tr></table></figure>
<p>系统输出：</p>
<blockquote>
<p>-rw-r–r– 1 root root 7 Nov 23 16:19 log<br>0000000   z   z   z   b   b   a   a<br>0000007</p>
</blockquote>
<p>表示此时文件 log 的大小没变，但是内容变成了 “zzzbbaa”，接着要清 log 了：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">: &gt; <span class="built_in">log</span>;</span><br><span class="line"><span class="comment"># 清空文件 log 的内容</span></span><br><span class="line">ls -l <span class="built_in">log</span>;od -c <span class="built_in">log</span>;</span><br><span class="line"><span class="comment"># 看文件 log 的大小和内容</span></span><br></pre></td></tr></table></figure>
<p>系统输出：</p>
<blockquote>
<p>-rw-r–r– 1 root root 0 Nov 23 16:26 log<br>0000000</p>
</blockquote>
<p>可以看出，文件 log 的大小变为 0，内容是空的，再接着看如果此时再通过文件描述符 3 写入内容会怎么样：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span> x &gt;&amp;3;</span><br><span class="line"><span class="comment"># 通过文件描述符 3 再次写入字符 "x"</span></span><br><span class="line">ls -l <span class="built_in">log</span>;od -c <span class="built_in">log</span>;</span><br><span class="line"><span class="comment"># 看文件 log 的大小和内容</span></span><br></pre></td></tr></table></figure>
<p>系统输出：</p>
<blockquote>
<p>-rw-r–r– 1 root root 6 Nov 23 16:32 log<br>0000000  \0  \0  \0  \0  \0   x<br>0000006</p>
</blockquote>
<p>看到没有，新写入的字符 “x” 直接写到了文件描述符 3 的光标位置，整个文件 log 的大小也还是通过文件描述符 3 写入的大小。这个实验应该完美地解释了前面我提到的日志文件的问题。实验最后，别忘了关闭文件描述符 3：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">exec</span> 3&gt;&amp;-;</span><br><span class="line">rm -f <span class="built_in">log</span>;</span><br></pre></td></tr></table></figure>
<h1 id="延展话题"><a href="#延展话题" class="headerlink" title="延展话题"></a>延展话题</h1><p>其实在 /proc 下能看到每个文件描述符打开文件时的参数，当然命令 <code>lsof</code> 也可以看到相关信息。来个实例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lsof -nP +f g | grep -E <span class="built_in">log</span>$;</span><br></pre></td></tr></table></figure>
<p>基于敏感信息原因系统输出就不贴了，我只把输出大概讲一下：</p>
<ul>
<li>FILE-FLAG 列的 W 代表写入</li>
<li>FILE-FLAG 列的 AP 代表 O_APPEND</li>
<li>FILE-FLAG 列的 LG 代表 O_LARGEFILE</li>
</ul>
<p>可以看出，大部分日志的打开方式都是有 AP 字样的，也就是说打开时是有 O_APPEND 参数的，这种日志文件直接用 <code>:&gt;log</code> 清是没有问题的，但是 OpenVPN 的这几个日志文件就不带 AP，所以这些个日志文件是不能用 <code>:&gt;log</code> 来清。</p>
<h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1><ul>
<li><a href="http://unix.stackexchange.com/questions/122929/emptying-a-file-without-disrupting-the-pipe-writing-to-it" target="_blank" rel="noopener">Emptying a file without disrupting the pipe writing to it</a></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>请我一杯咖啡吧！</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechat-reward.png" alt="老杨 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay-reward.png" alt="老杨 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          <div class="followme">
  <span>欢迎关注我的其它发布渠道</span>

  <div class="social-list">

      <div class="social-item">
          <a target="_blank" class="social-link" href="https://twitter.com/6fool">
            <span class="icon">
              <i class="fab fa-twitter"></i>
            </span>

            <span class="label">Twitter</span>
          </a>
      </div>

      <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/tags/CentOS/" rel="tag"># CentOS</a>
              <a href="/tags/Linux/" rel="tag"># Linux</a>
              <a href="/tags/OpenVPN/" rel="tag"># OpenVPN</a>
              <a href="/tags/log/" rel="tag"># log</a>
              <a href="/tags/log-append/" rel="tag"># log-append</a>
              <a href="/tags/O-APPEND/" rel="tag"># O_APPEND</a>
              <a href="/tags/O-WRONLY/" rel="tag"># O_WRONLY</a>
              <a href="/tags/O-LARGEFILE/" rel="tag"># O_LARGEFILE</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2016/11/%E5%9C%A8OpenVPN%E4%B8%8A%E5%90%AF%E7%94%A8AD-google-authenicator%E8%AE%A4%E8%AF%81/index.html" rel="prev" title="在OpenVPN上启用AD+Google Authenticator认证">
                  <i class="fa fa-angle-left"></i> 在OpenVPN上启用AD+Google Authenticator认证
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2016/11/%E4%B8%80%E6%AC%A1%E6%9C%AC%E5%9C%B0%E6%8F%90%E6%9D%83%E7%9A%84%E5%AE%9E%E6%88%98%E6%BC%94%E7%BB%83/index.html" rel="next" title="一次本地提权的实战演练">
                  一次本地提权的实战演练 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">老杨</span>
  </div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>

</body>
</html>
